export default {
  namespaced: true,
  state: {
    category: [],
    info: [],
    infoset: [],
    relation: [],
    categoryConfig: {},
    display: {},
    skin: []
  },
  getters: {
    defaultSkin: state => {
      return state.skin.length > 0 ? (state.skin.find(item => item.flag) || state.skin[0]) : null
    }
  },
  mutations: {
    setCategory(state, payload = []) {
      state.category = payload
    },
    setInfo(state, payload = []) {
      state.info = payload
    },
    setInfoset(state, payload = []) {
      state.infoset = payload
    },
    setRelation(state, payload = []) {
      state.relation = payload
    },
    setCategoryConfig(state, payload = {}) {
      state.categoryConfig = payload
    },
    setDisplay(state, payload = {}) {
      state.display = payload
    },
    setSkin(state, payload = []) {
      state.skin = payload
    }
  },
  actions: {
    init({ dispatch }) {
      dispatch('getCategory')
      dispatch('getInfo')
      dispatch('getInfoset')
      dispatch('getRelation')
      dispatch('getCategoryConfig')
      dispatch('getDisplay')
      dispatch('getSkin')
    },
    async getCategory({ commit, rootState }) {
      const result = await rootState.service.getCategory()
      if (result.flag && result.data) {
        commit('setCategory', result.data)
      }
    },
    async getInfo({ commit, rootState }) {
      const result = await rootState.service.getInfo()
      if (result.flag && result.data) {
        commit('setInfo', result.data.filter(item => item && item.uid))
      }
    },
    async getInfoset({ commit, rootState }) {
      const result = await rootState.service.getInfoset()
      if (result.flag && result.data) {
        commit('setInfoset', result.data.filter(item => item && item.uid))
      }
    },
    async getRelation({ commit, rootState }) {
      const result = await rootState.service.getRelation()
      if (result.flag && result.data) {
        commit('setRelation', result.data)
      }
    },
    async getCategoryConfig({ commit, rootState }) {
      const payload = {}
      const result = await rootState.service.getCategoryConfig()
      if (result.flag && result.data) {
        for (const item of result.data) {
          payload[item.id] = item
        }
        commit('setCategoryConfig', payload)
      }
    },
    async getDisplay({ commit, rootState }) {
      const result = await rootState.service.getDisplay()
      if (result.flag && result.data) {
        const display = {}
        for (const item of result.data) {
          if (item && item.info) {
            display[item.id] = {
              ...display[item.id],
              ...item.info,
              id: item.id,
              name: item.name,
              show: item.info.show && item.info.show.trim().toLowerCase() === 'true',
              readonly: item.info.readonly && item.info.readonly.trim().toLowerCase() === 'true',
              ...(item.info.htmltag ? { [item.info.htmltag]: true } : {})
            }
          }
        }
        commit('setDisplay', display)
      }
    },
    async getSkin({ commit, rootState }) {
      const result = await rootState.service.getSkin()
      if (result.flag && result.data) {
        commit('setSkin', result.data)
      }
    }
  }
}
